<template>
  <!-- 面包屑导航区域 -->
  <div>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getUserList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList"
            ></el-button> </el-input
        ></el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="姓名" prop="u_uname"></el-table-column>
        <el-table-column label="个人简介" prop="u_remark"></el-table-column>
        <el-table-column label="用户账号" prop="u_user"></el-table-column>
        <el-table-column label="用户星级" prop="u_star"></el-table-column>
        <el-table-column label="金额" prop="u_balance">
           <template slot-scope="scope" >
            <div class="userlook">
            <span>{{scope.row.u_balance}}</span>
            <span @click="showMony(scope.row.id)">查看</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用户头像" prop="u_pic">
          <template slot-scope="scope">
            <img style="width:70px;height:70px;border-radius:50%" :src="scope.row.u_pic" alt="">
          </template>
        </el-table-column>
        <el-table-column
          label="上次登录时间"
          prop="u_ endtime"
        ></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope"
            ><!-- 修改，删除，分配角色按-->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除，-->
            <!-- <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button> -->
            <!-- 分配角色按-->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[  1,2,5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 弹出钱包 -->
     <el-dialog
  title="金额明细"
  :visible.sync="dialogVisibleMony"
  width="100%">
   <el-table
      :data="monylist"
      style="width: 100%">
      <el-table-column
        prop="ic_uname"
        label="用户名字"
        width="180">
      </el-table-column>
      <el-table-column
        prop="ic_money"
        label="变动金额"
        width="180">
      </el-table-column>
      <el-table-column
        prop="ic_explan"
        label="变动描述"
        width="180">
      </el-table-column>
      <el-table-column
        prop="ic_time"
        label="变动时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="ic_pname"
        label="任务名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="ic_ordersn"
        label="任务单号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="ic_remark"
        label="任务备注"
        width="180">
      </el-table-column>
      <el-table-column
        prop="ic_type"
        label="金额增减"
        width="180">
      </el-table-column>
      <el-table-column
        prop="totalCount"
        label="总条数"
        width="180">
      </el-table-column>
    </el-table>
</el-dialog>
    <!-- 修改用户 -->
    <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="微信名称">
          <el-input v-model="editForm.u_uname" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户账号">
          <el-input v-model="editForm.u_user" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="u_pass" required>
          <el-input v-model="editForm.u_pass"></el-input>
        </el-form-item>
        <!-- <el-form-item label="头像" prop="u_pic">
          <el-input v-model="editForm.u_remark"></el-input>
        </el-form-item> -->
        
        <el-form-item label="个人简介" prop="u_remark" required>
          <el-input v-model="editForm.u_remark"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="u_phone" required>
          <el-input v-model="editForm.u_phone"></el-input>
        </el-form-item>
        <el-form-item label="用户星级" prop="u_star" required>
          <el-input v-model.number="editForm.u_star"></el-input>
        </el-form-item>
        <el-form-item label="金额" prop="u_balance" required>
          <el-input v-model.number="editForm.u_balance"></el-input>
        </el-form-item>
        <el-form-item label="用户头像" prop="u_pic" >
          <uploadImg @clay_emit='clay_emit' :clay_img='editForm.u_pic'></uploadImg>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import uploadImg from '@/components/element/upload.vue'
export default {
  components:{
    uploadImg
  },
  data() {
    //验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      //验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if (regEmail.test(value)) {
        //合法的邮箱
        return cb();
      }
      cb(new Error("请输入合法的邮箱"));
    };

    //验证手机的规则
    var checkMobile = (rule, value, cb) => {
      //验证手机的正则
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if (regMobile.test(value)) {
        //手机验证通过
        return cb();
      }
      cb(new Error("请输入合法的手机号"));
    };
    return {
      //查看钱包
      monylist:[],
      dialogVisibleMony: false,
      //时间线
      reverse: true,
      //任务列表标签
    
      //获取用户列表参数的对象
      queryInfo: {
        query: "",
        //当前页数
        pagenum: 1,
        //当前每页显示多少数据
        pagesize: 10,
      },
      userlist: [],
      total: 0,
      //控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      //控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      //查询到用户信息对象
      editForm: {},
      //修改表单的验证规则对象
      editFormRules: {
        u_uname: [
          { required: true, message: "微信名称", trigger: "blur" },
          {min: 3,max: 30,message: "长度在 3 到 30 个字符",trigger: "blur", },
        ],
        u_user: [
          { required: true, message: "用户账户", trigger: "blur" },
          {min: 3, max: 30,message: "长度在 3 到 30 个字符",
            trigger: "blur",} ],
       u_pass: [
           { required: true, message: "请输入密码", trigger: "blur" },
          {min: 3, max: 255,message: "长度在 3 到 255个字符", trigger: 'blur'},
        ],
        u_remark: [
          { required: true, message: "请填写简介", trigger: "blur" },
          {min: 3, max: 255,message: "长度在 3 到 255个字符"},
        ],
        u_phone: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
        u_star: [
          { required: true, trigger: "blur" },
          { type: "number", message: "必须为数字值", trigger: 'blur' },
        ],
        u_balance: [
           { required: true, message: '金额不能为空', trigger: 'blur'}
        ]},
    };
  },
  created() {
    this.getUserList();
    // this.getUser();
  },
  methods: {
    //子组件上传文件的地址
    clay_emit(val){
      this.editForm.u_pic=val
    },
    // async getUser() {
    //   const { data: res } = await this.$http.get("/admin/getAllUser/",{currentpage:this.currentpage});
    //   console.log(33333,res)
    // },
    //查看钱包
    async showMony(id){
      this.dialogVisibleMony = true
      const {data:res}= await this.$http.get('/admin/userDetails',{id,'page-size':15,currentpage:1})
      this.monylist=res.data[0].data
      console.log(this.monylist)
      
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //获取所有用户
    async getUserList() {
      const { data: res } = await this.$http.get("/admin/getAllUser", {
        currentpage: this.queryInfo.pagenum,
        'page-size':this.queryInfo.pagesize
      });
      this.userlist = res.data[0].data;
     this.total = res.data[1].totalCount;
      // this.total = res.data.total;
      if (res.code == 200) {
        return this.$message.success(res.msg);
      }
    },

    // //页码获取
    // async getUserList() {
    //   const { data: res } = await this.$http.get("/admin/getAllUser", {
    //     params: this.queryInfo,
    //   });
    //   console.log(33+res)
    //   if (res.code == 200) {
    //     return this.$message.success(res.msg);
    //   }
    //   this.userlist = res.data.users;
    //   this.total = res.data.total;
    // },
    //监听数据条码改变的事件
    handleSizeChange(newSize) {
      console.log(777777,newSize)
      this.queryInfo.pagesize = newSize;
      this.getUserList();
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getUserList();
    },
    //监听switch开关状态的改变
    async userStateChanged(userinfo) {
      console.log(userinfo);
      const { data: res } = await this.$http.put(
        `users/${userinfo.id}/state/${userinfo.mg_state}`
      );
      if (res.meta.status !== 200) {
        userinfo.mg_state = !userinfo.mg_state;
        return this.$message.err("更新用户状态失败！");
      }
      this.$message.success("更新用户状态成功！");
    },
    //监听添加用户框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //点击按钮，添加新用户
    // addUser() {
    //   this.$refs.addFormRef.validate(async (valid) => {
    //     if (!valid) {
    //       return;
    //     }
    //     //如果没有retur回去就说明可以添加
    //     const { data: res } = await this.$http.post("/admin/getAllUser", this.addForm);
    //     if (res.meta.status !== 201) {
    //       return this.$message.error("添加用户失败");
    //     }
    //     this.$message.success("添加用户成功");
    //     //添加成功隐藏对话框
    //     this.addDialogVisible = false;
    //     //重新获取用户列表数据
    //     this.getUserList();
    //   });
    // },
    async showEditDialog(id) {
      //展示编辑用户的对话框
      this.editDialogVisible = true;
      console.log(id);
      const { data: res } = await this.$http.get("/admin/getOneUser", { id });
      // console.log(99999,res)
      if (res.code !== 200) {
        return this.$message.error("用户信息查询失败");
      }
      this.end={...res.data}
      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    editDialogClosed() {
      //监听修改用户对话框的关闭事件
      this.$refs.editFormRef.resetFields();
    },
    //修改信息并提交
    editUserInfo() {
      let key = this.editForm
      let i =this.end
      if(key.u_user == i.u_user &&key.u_uname==i.u_uname && key.u_phone== i.u_phone&&key.u_pass==i.u_pass && key.u_balance==i.u_balance && key.u_remark==i.u_remark && key.u_pic==i.u_pic){
        this.$message.success("暂未修改无法提交");
        return false
      }
      let obj = {
        id:key.id,
        u_user:key.u_user,
        u_uname:key.u_uname,
        u_phone:key.u_phone,
        u_pass:key.u_pass,
        u_balance:key.u_balance,
        u_remark:key.u_remark,
        u_pic:key.u_pic
      }
      this.$refs.editFormRef.validate(async (valid) => {
        // console.log(this.$refs.editFormRef,9999)
        if (!valid) return;
        //如果没有return说明验证通过发起修改用户信息
        const { data: res } = await this.$http.post(
          "/admin/editUserInfo" ,obj
        );
        if (res.code !== 200) {
          return this.$message.error("更新用户信息失败");
        }
        //如果没有返回失败 1.关闭对话框 2.刷新数据列表3.提示修改成功
        this.editDialogVisible = false;
        this.getUserList();
        this.$message.success("更新用户成功");
      });
    },
    //根据id删除对应的用户信息
    // async removeUserById(id) {
    //   console.log(id, 9999999);
    //   const confirmResult = await this.$confirm(
    //     "此操作将永久删除该任务, 是否继续?",
    //     "提示",
    //     {
    //       confirmButtonText: "确定",
    //       cancelButtonText: "取消",
    //       type: "warning",
    //     }
    //   )
    //     .then(async (con) => {
    //       let res = await this.$http.get("/admin/delByProject", {
    //         id,
    //       });
    //       console.log(999999, id);
    //       this.$message.success("删除任务成功");
    //       this.loadtask();
    //     })
    //     .catch((err) => {
    //       this.$message.error("删除失败");
    //     });
    // },
  },
};
</script>
<style lang="less" scoped>
.userlook span {
  margin: 5px;
}
.userlook :last-child:hover {
  color: blue;
  cursor: pointer;
}
</style>